package com.ysh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysh.mapper.OffencesMapper;
import com.ysh.pojo.Crimes;
import com.ysh.pojo.CrimesMapVo;
import com.ysh.pojo.CrimesVo;
import com.ysh.pojo.R;
import com.ysh.service.CrimesService;
import com.ysh.mapper.CrimesMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.sql.Wrapper;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 嚴嚴嚴
 * @description 针对表【crimes】的数据库操作Service实现
 * @createDate 2023-12-11 17:52:53
 */
@Service
public class CrimesServiceImpl extends ServiceImpl<CrimesMapper, Crimes>
        implements CrimesService {
    @Resource
    private CrimesMapper crimesMapper;
    @Resource
    private OffencesMapper offencesMapper;

    @Resource
    CrimesService crimesService;

    /**
     * - 按年份统计每年风险指数最高的3个城市
     *
     * @return
     */
    @Override
    public R risk() {
//拿到年份集合
        List<String> yearlist = crimesMapper.Yearlist();


        ArrayList<CrimesMapVo> crimesMapVolist = new ArrayList<>();
        //遍历年份 集合
        yearlist.forEach(year -> {
                    CrimesMapVo crimesMapVo = new CrimesMapVo();
                    crimesMapVo.setYear(year);
                    int i = 0;
                    //根据年份拿到该年前三的犯罪城市
                    List<CrimesVo> crimesVos = crimesMapper.selectTop3(year);
                    for (CrimesVo crimesVo : crimesVos) {
                        i++;
                        crimesVo.setRank(String.valueOf(i));
                    }
                    crimesMapVo.setRiskTop3(crimesVos);
                    crimesMapVolist.add(crimesMapVo);
                }
        );
        return R.success(crimesMapVolist);
    }

    /**
     * 添加
     *
     * @param crimes
     * @return
     */
    @Override
    public R add(Crimes crimes) {
        int insert = crimesMapper.add(crimes);
        Integer integer = null;
        if (insert > 0) {
            integer = crimesMapper.selectByoffenceCode(crimes.getOffence_code());
        }


        return  R.success(integer);
    }
}




